/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-08
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-02-08
 * @Description: 
 */
class User {
  final String userId;
  final String phoneNumber;
  final String username;
  final String password;
  final String nickname;
  final String avatarUrl;
  final int gender;
  final String birthday;
  final String region;
  final String regionCode;
  final String signature;
  final int status;
  final int addType;

  User({
    required this.userId,
    required this.phoneNumber,
    required this.username,
    required this.password,
    required this.nickname,
    required this.avatarUrl,
    required this.gender,
    required this.birthday,
    required this.region,
    required this.regionCode,
    required this.signature,
    required this.status,
    required this.addType,
  });

  /// 从 JSON 构造 `User` 对象
  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      userId: json['userId'] ?? json['_id'] ?? '',
      phoneNumber: json['phoneNumber'] ?? '',
      username: json['username'] ?? '',
      password: json['password'] ?? '',
      nickname: json['nickname'] ?? '',
      avatarUrl: json['avatarUrl'] ?? '',
      gender: json['gender'] ?? 0,
      birthday: json['birthday'] ?? '',
      region: json['region'] ?? '中国大陆',
      regionCode: json['regionCode'] ?? '86',
      signature: json['signature'] ?? '',
      status: json['status'] ?? 1,
      addType: json['addType'] ?? 1,
    );
  }

  /// 转换为 JSON 结构
  Map<String, dynamic> toJson() {
    return {
      'userId': userId,
      'phoneNumber': phoneNumber,
      'username': username,
      'password': password,
      'nickname': nickname,
      'avatarUrl': avatarUrl,
      'gender': gender,
      'birthday': birthday,
      'region': region,
      'regionCode': regionCode,
      'signature': signature,
      'status': status,
      'addType': addType,
    };
  }

  /// `copyWith` 方法，用于创建修改某些字段的新对象
  User copyWith({
    String? userId,
    String? phoneNumber,
    String? username,
    String? password,
    String? nickname,
    String? avatarUrl,
    int? gender,
    String? birthday,
    String? region,
    String? regionCode,
    String? signature,
    int? status,
    int? addType,
  }) {
    return User(
      userId: userId ?? this.userId,
      phoneNumber: phoneNumber ?? this.phoneNumber,
      username: username ?? this.username,
      password: password ?? this.password,
      nickname: nickname ?? this.nickname,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      gender: gender ?? this.gender,
      birthday: birthday ?? this.birthday,
      region: region ?? this.region,
      regionCode: regionCode ?? this.regionCode,
      signature: signature ?? this.signature,
      status: status ?? this.status,
      addType: addType ?? this.addType,
    );
  }
}
